<?php

namespace App\Api\Controllers\v1;

use App\Http\Controllers\ApiController;
use App\Http\Response\ApiResponse;
use App\Models\GoodOrder;
use App\Services\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use hg\apidoc\annotation as Apidoc;

/**
 * 商品订单
 * @Apidoc\Group("user")
 * @Apidoc\Sort(1)
 */
class OrderController extends ApiController
{

    /**
     * 商品订单列表
     * @Apidoc\Url("/api/v1/order/list")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("state", type="int", default="-1" , desc="-1=全部,0=待支付,1=待发货,2=待收货,3=已完成,4=已评价,5=已退款, 默认值:-1")
     * @Apidoc\Query(ref="queryPage")
     * @Apidoc\Returned(ref="listResult")
     * @return ApiResponse
     */
    public function list(): ApiResponse
    {
        $data = GoodOrder::getList($this->user_id);
        return $this->response()->data($data);
    }


    /**
     * 商品订单详情
     * @Apidoc\Url("/api/v1/order/detail/:order_no")
     * @Apidoc\Method("GET")
     * @Apidoc\RouteParam("order_no",type="string", require=true, desc="订单号")
     * @Apidoc\Returned("detail", ref="App\Models\GoodOrder")
     * @param Request $request
     * @return ApiResponse
     */
    public function detail(Request $request): ApiResponse
    {
        $order_no = $request->route('order_no');
        $result = GoodOrder::getDetail($order_no);
        return $this->response()->data($result);
    }

    /**
     * 订单确认收货
     * @Apidoc\Url("/api/v1/order/confirm/:order_no")
     * @Apidoc\Method("POST")
     * @Apidoc\RouteParam("order_no",type="string", require=true, desc="订单号")
     * @param Request $request
     * @return ApiResponse
     * @throws \Throwable
     */
    public function confirm(Request $request): ApiResponse
    {
        $order_no = $request->route("order_no");
        DB::beginTransaction();
        try {
            $res = OrderService::confirmGoodOrder($order_no);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            Log::error($e->getMessage());
            return $this->response()->error($e->getMessage());
        }
        if ($res){
            return $this->response()->success("已确认收货");
        }else{
            return $this->response()->error('确认失败!');
        }
    }

    /**
     * 申请退款
     * @Apidoc\Url("/api/v1/order/refund/:order_no")
     * @Apidoc\Method("POST")
     * @Apidoc\RouteParam("order_no",type="string", require=true, desc="订单号")
     * @param Request $request
     * @return ApiResponse
     * @throws \Throwable
     */
    public function refund(Request $request): ApiResponse
    {
        $order_no = $request->route("order_no");
        $order = GoodOrder::whereOrderNo($order_no)->first();
        if (!$order){
            return $this->response()->error('暂无此订单!');
        }
        if ($order->state==3||$order->state==4){
            return $this->response()->error('此订单不可申请退款!');
        }
        if ($order->state==5){
            return $this->response()->error('此订单已退款!');
        }
        $order->refund_no = build_order_no();
        $order->is_apply = 1;
        $res = $order->save();
        if ($res){
            return $this->response()->success("已申请退款");
        }else{
            return $this->response()->error('申请失败!');
        }
    }


    /**
     * 员工扫码核销订单
     * @Apidoc\Url("/api/v1/order/scan/:order_no")
     * @Apidoc\Method("POST")
     * @Apidoc\RouteParam("order_no",type="string", require=true, desc="订单号")
     * @param Request $request
     * @return ApiResponse
     * @throws \Throwable
     */
    public function scan(Request $request): ApiResponse
    {
        $order_no = $request->route("order_no");
        DB::beginTransaction();
        try {
            $user_id = $this->user_id;
            $res = OrderService::scanGoodOrder($user_id,$order_no);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            Log::error($e->getMessage());
            return $this->response()->error($e->getMessage());
        }
        if ($res){
            return $this->response()->success("核销成功");
        }else{
            return $this->response()->error('核销成功!');
        }
    }

}
